package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.MS;
import com.example.entity.Recruitment;
import com.example.entity.Student;
import com.example.service.RecruitmentService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("recruitments")
public class RecruitmentController {

    @Autowired
    private RecruitmentService recruitmentService;

    /**
     * 招聘单位登录
     * @return
     */
    @PostMapping("/loginRecruitment")
    public R login(HttpServletRequest request, @RequestBody Recruitment recruitment){
        LambdaQueryWrapper<Recruitment> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(recruitment.getUsername()),Recruitment::getUsername,recruitment.getUsername());
        lqw.eq(Strings.isNotEmpty(recruitment.getPassword()),Recruitment::getPassword,recruitment.getPassword());

        Recruitment one = recruitmentService.getOne(lqw);

        if (one ==null){
            return R.error("登录失败！");
        }else if (! "true".equals(one.getStatus())){
            return R.error("登录失败！账号被警用!");
        }else {
            return R.success("登录成功！",one);
        }
    }


    /**
     * 招聘单位分页查询
     * @return
     */
    @GetMapping("/getPageRecruitment/{current}/{size}")
    public R getPageRecruitment(@PathVariable int current,@PathVariable int size,Recruitment recruitment){

        IPage page =new Page(current,size);
        LambdaQueryWrapper<Recruitment> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(recruitment.getCommercialName()),Recruitment::getCommercialName,recruitment.getCommercialName());


        IPage page1 = recruitmentService.page(page,lqw);

        return R.success("招聘单位分页查询！",page1);
    }

    /**
     * 添加招聘单位
     * @return
     */
    @PostMapping("/addRecruitment")
    public R addRecruitment(@RequestBody Recruitment recruitment){

        LambdaQueryWrapper<Recruitment> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(recruitment.getCommercialName()),Recruitment::getCommercialName,recruitment.getCommercialName());
        //1.查询数据库此商户名是否存在
        List<Recruitment> list = recruitmentService.list(lqw);

        //2.不存在在添加
        if (list.size()==0){
            boolean save = recruitmentService.save(recruitment);
            if (save){
                return R.success("添加招聘单位成功！",null);
            }else {
                return R.error("添加招聘单位失败！");
            }
        }else {
            //3.存在
            return R.error("商户名存在！添加失败！");
        }



    }

    /**
     * 通过id查询招聘单位
     * @return
     */
    @GetMapping("/getRecruitmentById")
    public R getRecruitmentById(@PathVariable int id){

        LambdaQueryWrapper<Recruitment> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Recruitment::getId,id);
        Recruitment one = recruitmentService.getOne(lqw);
        return R.success("通过id查询招聘单位",one);
    }

    /**
     * 通过id修改招聘单位
     * @return
     */
    @PutMapping("/updateRecruitment")
    public R updateRecruitment(@RequestBody Recruitment recruitment){
        boolean b = recruitmentService.updateById(recruitment);
        if (b){
           return R.success("通过id修改招聘单位成功",recruitment);
        }else {
           return R.error("通过id修改招聘单位失败！");
        }
    }

    /**
     * 通过id删除招聘单位
     * @return
     */
    @DeleteMapping("/deleteRecruitment/{id}")
    public R deleteRecruitment(@PathVariable int id){

        boolean b = recruitmentService.removeById(id);
        if (b){
           return R.success("通过id删除招聘单位成功",id);
        }else {
           return R.error("通过id删除招聘单位失败！");
        }
    }

    /**
     * 根据id查询招聘单位信息
     * @return
     */
    @GetMapping("/getMyself/{id}")
    public R getMyself(@PathVariable int id){

        Recruitment one = recruitmentService.getById(id);
        return R.success("根据id查询招聘单位信息(修改回显)",one);
    }

    /**
     * 通过id修改招聘单位账号状态
     * @return
     */
    @PutMapping("/updateRecruitmentStatus")
    public R updateRecruitmentStatus(@RequestBody Recruitment recruitment){
        boolean b = recruitmentService.updateById(recruitment);
        if (b){
            return R.success("通过id修改招聘单位账号状态成功",recruitment);
        }else {
            return R.error("通过id修改招聘单位账号状态失败！");
        }
    }



}

